package com.lineage.server.clientpackets;

import com.lineage.config.ConfigOther;
import com.lineage.echo.ClientExecutor;
import com.lineage.list.BadNamesList;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1Clan;
import com.lineage.server.serverpackets.S_CharTitle;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.world.World;
import com.lineage.server.world.WorldClan;
import java.util.logging.Level;
import java.util.logging.Logger;

public class C_Title extends ClientBasePacket {
  private static final String C_TITLE = "[C] C_Title";
  
  private static Logger _log = Logger.getLogger(C_Title.class.getName());
  
  public void start(byte[] decrypt, ClientExecutor client) {
    read(decrypt);
    L1PcInstance pc = client.getActiveChar();
    if (pc == null)
      return; 
    String charName = readS();
    String title = readS();
    if (charName.isEmpty() || title.isEmpty()) {
      pc.sendPackets((ServerBasePacket)new S_ServerMessage(196));
      return;
    } 
    L1PcInstance target = World.get().getPlayer(charName);
    if (target == null)
      return; 
    if (pc.isGm()) {
      changeTitle(target, title);
      return;
    } 
    if (isClanLeader(pc)) {
      if (pc.getId() == target.getId()) {
        if (pc.getLevel() < 10) {
          pc.sendPackets((ServerBasePacket)new S_ServerMessage(197));
          return;
        } 
        changeTitle(pc, title);
      } else {
        if (pc.getClanid() != target.getClanid()) {
          pc.sendPackets((ServerBasePacket)new S_ServerMessage(201));
          return;
        } 
        if (target.getLevel() < 10) {
          pc.sendPackets((ServerBasePacket)new S_ServerMessage(202, charName));
          return;
        } 
        if (BadNamesList.get().isBadName(title)) {
          pc.sendPackets((ServerBasePacket)new S_ServerMessage("此称号无效名称"));
          return;
        } 
        changeTitle(target, title);
        L1Clan clan = WorldClan.get().getClan(pc.getClanname());
        if (clan != null) {
          L1PcInstance[] array;
          int n = (array = clan.getOnlineClanMember()).length;
          int i = 0;
          while (i < n) {
            L1PcInstance clanPc = array[i];
            clanPc.sendPackets((ServerBasePacket)new S_ServerMessage(203, pc.getName(), charName, title));
            i++;
          } 
        } 
      } 
    } else if (pc.getId() == target.getId()) {
      if (pc.getClanid() != 0 && !ConfigOther.CLANTITLE) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(198));
        return;
      } 
      if (target.getLevel() < 40) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(200));
        return;
      } 
      if (BadNamesList.get().isBadName(title)) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage("此称号无效名称"));
        return;
      } 
      changeTitle(pc, title);
    } else if (pc.isCrown()) {
      if (pc.getClanid() == target.getClanid()) {
        if (target.getClanRank() == 10 || target.getClanRank() == 4 || target.getClanRank() == 3) {
          pc.sendPackets((ServerBasePacket)new S_ServerMessage(2065));
          return;
        } 
        changeTitle(target, title);
        L1Clan clan = WorldClan.get().getClan(pc.getClanname());
        if (clan != null) {
          L1PcInstance[] array;
          int n = (array = clan.getOnlineClanMember()).length;
          int i = 0;
          while (i < n) {
            L1PcInstance clanPc = array[i];
            clanPc.sendPackets((ServerBasePacket)new S_ServerMessage(203, pc.getName(), charName, title));
            i++;
          } 
        } 
      } 
    } else if (pc.getClanid() == target.getClanid()) {
      if (pc.getClanRank() == 9 || pc.getClanRank() == 6) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(2474));
      } else {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(2143));
      } 
      return;
    } 
  }
  
  private void changeTitle(L1PcInstance pc, String title) {
    int objectId = pc.getId();
    pc.setTitle(title);
    pc.sendPackets((ServerBasePacket)new S_CharTitle(objectId, title));
    pc.broadcastPacketAll((ServerBasePacket)new S_CharTitle(objectId, title));
    try {
      pc.save();
    } catch (Exception e) {
      _log.log(Level.SEVERE, e.getLocalizedMessage(), e);
    } 
  }
  
  private boolean isClanLeader(L1PcInstance pc) {
    boolean isClanLeader = false;
    if (pc.getClanid() != 0) {
      L1Clan clan = WorldClan.get().getClan(pc.getClanname());
      if (clan != null && pc.isCrown() && pc.getId() == clan.getLeaderId())
        isClanLeader = true; 
    } 
    return isClanLeader;
  }
  
  public String getType() {
    return "[C] C_Title";
  }
}
